// Menu Schame


var mongoose = require('mongoose')

var MenuSchema = new mongoose.Schema({
	name: {
		unique: true,
		type: String
	},
	parentid: String,
	level: {
		type: Number,
		default: 1
	},
	url: String,
	description: String,
	status: {
		type: Number,
		default: 0
	},
	meta: {
		createAt: {
			type: Date,
			default: Date.now()
		},
		updateAt: {
			type: Date,
			default: Date.now()
		}
	}
})

MenuSchema.pre('save', function(next){
	var user = this
	if(this.isNew){
		this.meta.createAt = this.meta.updateAt = Date.now()
	}
	else{
		this.meta.updateAt = Date.now()
	}
	next()
})

MenuSchema.statics = {
	fetch: function (cb) {
		return this
			.find({})
			.sort('meta.updateAt')
			.exec(cb)
	},
	findById: function (id, cb) {
		return this
			.findOne({_id: id})
			.exec(cb)
	}
}

module.exports = MenuSchema